<?php

namespace App\Listeners;

use App\Constant\JpushCode;
use App\Constant\SmsTemplate;
use App\Events\JpushNoticeEvent;
use App\Events\SmsSendEvent;
use App\Events\UserLoanApplyEvent;
use App\Models\LoanApply;
use App\Models\RepaySummary;
use App\Models\User;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class UserLoanApplyEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * @param UserLoanApplyEvent $event
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \MathPHP\Exception\BadParameterException
     * @throws \ReflectionException
     */
    public function handle(UserLoanApplyEvent $event)
    {
        $apply = $event->apply;
        $globalStatus = UserGlobal::STATUS_LOAN_MONEY_PROCESSING;
        $user = User::find($event->apply->user_id);
        $smsParams = [];
        switch (intval($apply->loan_status)) {
            //放款中
            case  LoanApply::LOAN_STATUS_PROCESSING:
                $globalStatus = UserGlobal::STATUS_LOAN_MONEY_PROCESSING;
                break;
            //放款失败 换卡后处理结果也是失败
            case LoanApply::LOAN_STATUS_FAIL:
            case LoanApply::LOAN_HANDLE_CHANGE_CARD_FAIL:
                $globalStatus = UserGlobal::STATUS_LOAN_MONEY_FAIL;

                //极光推送
                if (!empty($user->globalStatus) && $user->globalStatus->jpush_registration_id) {
                    event(new JpushNoticeEvent([
                        'title' => '放款失败提醒',
                        'body' => '您有一笔借款申请放款失败，点击了解相关信息',
                        'audience' => JpushCode::AUDIENCE_TYPE_TWO,
                        'to' => [$user->globalStatus->jpush_registration_id],
                        'extras' => [
                            'jump_type' => 2,
                        ]
                    ]));
                }
                //短信推送
                if(SmsTemplate::TEMPLATE_CONFIRM_LOAN_APPLY_FAIL){
                    SmsTemplate::send(SmsTemplate::TEMPLATE_CONFIRM_LOAN_APPLY_FAIL,$user->phone_num,[
                        'name' => $user->valid->name,
                        'amount' => $apply->amount,
                        'term' => $apply->period,
                    ]);
                }
                break;
            //需要换卡，换卡处理中
            case LoanApply::LOAN_CHANGE_CARD_HANDLING:
            case LoanApply::LOAN_CHANGE_CARD :
                $globalStatus = UserGlobal::STATUS_LOAN_CHANGE_CARD;
                break;
            // 放款成功 换卡后处理结果为成功
            case LoanApply::LOAN_STATUS_SUCCESS:
            case LoanApply::LOAN_HANDLE_CHANGE_CARD_SUCCESS:
                $globalStatus = UserGlobal::STATUS_REPAY_INIT;
                Log::debug('初始化还款：'. json_encode($apply, JSON_UNESCAPED_UNICODE));
                //初始化还款
                RepaySummary::initRepaySummary($apply);

                //发短信推送
                if (SmsTemplate::TEMPLATE_LOAN_SUCCESS && $user->valid) {
                    SmsTemplate::send(SmsTemplate::TEMPLATE_LOAN_SUCCESS, $user->phone_num, [
                        'name' => $user->valid->name,
                        'amount' => strval($apply->amount),
                    ]);
                }
                //极光推送
                if (!empty($user->globalStatus) && $user->globalStatus->jpush_registration_id) {
                    event(new JpushNoticeEvent([
                        'title' => '放款成功提醒',
                        'body' => sprintf('您的%s期%s元借款也放款至您的账户，请您前往“我的卡贷”了解相关信息', intval($apply->period), intval($apply->amount)),
                        'audience' => JpushCode::AUDIENCE_TYPE_TWO,
                        'to' => [$user->globalStatus->jpush_registration_id],
                        'extras' => [
                            'jump_type' => 2,
                        ]
                    ]));
                }
                break;
            //默认放款中
            default:
                $globalStatus = UserGlobal::STATUS_LOAN_MONEY_PROCESSING;
                break;
        }
        UserGlobal::where('uid', $apply->user_id)->update(['global_status' => $globalStatus]);
    }
}
